﻿using System.Xml;
using System.IO;
using System;
using System.Collections.Generic;
using LoveMusic.Models;
using System.Threading;
using System.Net;
using AtlantisClientWPF.Bins;
using LoveMusic.Helpers;

namespace AtlantisClientWPF.APIS
{
    public class BillboardAPI
    {
        List<SongBinz> _allSongs = new List<SongBinz>();

        const string _urlRNBHipHop = "http://www.billboard.com/rss/charts/r-b-hip-hop-songs";
        const string _urlRock = "http://www.billboard.com/rss/charts/rock-songs";
        const string _urlPop = "http://www.billboard.com/rss/charts/pop-songs";
        const string _urlClub = "http://www.billboard.com/rss/charts/dance-club-play-songs";
        const string _urlCountry = "http://www.billboard.com/rss/charts/country-songs";
       
        const string _songsUK = "http://www.billboard.com/rss/charts/united-kingdom-songs";
        const string _BillBoard200 = "http://www.billboard.com/rss/charts/billboard-200";
        const string _hot100 = "http://www.billboard.com/rss/charts/hot-100";
        const string _digital100 = "http://www.billboard.com/rss/charts/digital-songs";
        const string _radioSongs = "http://www.billboard.com/rss/charts/radio-songs";
        const string _rnb = "http://www.billboard.com/rss/charts/r-b-hip-hop-songs";
        const string _unitedKingdom = "http://www.billboard.com/rss/charts/united-kingdom-songs";
        const string _ilike = "http://www.billboard.com/rss/charts/ilike-profiles-most-added";
        const string _rapsongs = "http://www.billboard.com/rss/charts/rap-songs";

        public List<SongBinz> GetSongzzz()
        {
            _allSongs.AddRange(GetSongs(_urlCountry, 15));
            _allSongs.AddRange(GetSongs(_urlRNBHipHop, 10));
            _allSongs.AddRange(GetSongs(_urlPop, 3));
            _allSongs.AddRange(GetSongs(_urlClub, 6));


            _allSongs.AddRange(GetSongs(_songsUK, 0));

            _allSongs.AddRange(GetSongs(_hot100, 0));
            _allSongs.AddRange(GetSongs(_digital100, 0));
            _allSongs.AddRange(GetSongs(_radioSongs, 0));
            _allSongs.AddRange(GetSongs(_unitedKingdom, 0));
            _allSongs.AddRange(GetSongs(_ilike, 0));

            _allSongs.AddRange(GetSongs(_rnb, 10));
            _allSongs.AddRange(GetSongs(_rapsongs, 2));
            _allSongs.AddRange(GetSongs(_urlRock, 1));
            _allSongs.AddRange(GetSongs(_BillBoard200, 0));

            return _allSongs;
        }

        private static List<SongBinz> GetSongs(string url, int categoryID)
        {
            List<SongBinz> songs = new List<SongBinz>();

            string xml = WebHelper.GetResponseStandard(url);
            if (xml == null)
                return songs;

            Thread.Sleep(2000);
            string strElement = string.Empty;
            string[] parts;
            string artistName = string.Empty;
            string songName = string.Empty;
            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc = load(xml, xmlDoc, url);

            if (xmlDoc != null)
            {
                foreach (XmlElement element in xmlDoc.GetElementsByTagName("item"))
                {
                    try
                    {
                        SongBinz song = new SongBinz();
                        strElement = element.GetElementsByTagName("title").Item(0).InnerXml;
                        strElement = strElement.Split(new string[] { ":" }, StringSplitOptions.None)[1];
                        parts = strElement.Split(new string[] { "," }, StringSplitOptions.None);
                        song.SongName = parts[0].Trim();
                        song.ArtistName = parts[1].Trim();
                        if (categoryID != 0)
                            song.Category = categoryID;

                        songs.Add(song);
                    }
                    catch (Exception eX)
                    {
                        continue;
                    }
                }
                songs.Reverse();
            }
            return songs;
        }

        private static int countAttempts = 0;

        private static XmlDocument load(string xml, XmlDocument xmlDoc, string url)
        {
            try
            {
                xmlDoc.Load(new StringReader(xml));
                return xmlDoc;
            }
            catch (WebException webE)
            {
                return null;
            }
            catch (Exception ex)
            {
                Thread.Sleep(12000);

                xml = WebHelper.GetResponse(url);
                if (countAttempts++ < 3)
                    return load(xml, xmlDoc, url);
                else
                    return null;
            }
        }

    }
}